package com.anolesoft.yr.mbus.pre.crtl;

import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.pre.dto.PreOrderUpdateDTO;
import com.anolesoft.yr.mbus.pre.service.PreOrderService;
import com.anolesoft.yr.mbus.pre.vo.PreOrderQueryVO;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@CrossOrigin
@RequestMapping(value = "/preOrder")
public class PreOrderController {

    @Autowired
    PreOrderService preOrderService;

    //生成报价单
    @RequestMapping(value = "/updatePreOrderState1", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updatePreOrderState1(String objuid) {
        String str = "";
        try {
            PreOrderUpdateDTO preOrderUpdateDTO = new PreOrderUpdateDTO();
            preOrderUpdateDTO.setObjuid(objuid);
            str = preOrderService.updatePreOrderState1(preOrderUpdateDTO);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }

        return new AjaxResult(200, str);
    }

    //查询报价单信息（审批）
    @RequestMapping(value = "/queryPreOrderAll2", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryPreOrderAll2(String preInquiryUid) throws Exception {
        List preOrderQueryVOList = preOrderService.queryPreOrderAll2(preInquiryUid);

        String jsons = "";
        jsons += "[";
        if (preOrderQueryVOList.size() > 0) {
            for (int i = 0; i < preOrderQueryVOList.size(); i++) {
                PreOrderQueryVO preOrderQueryVO = (PreOrderQueryVO) preOrderQueryVOList.get(i);
                jsons += new Gson().toJson(preOrderQueryVO);
                if (i < (preOrderQueryVOList.size() - 1)) {
                    jsons += ",";
                }
            }
        }
        jsons += "]";

        return new AjaxResult(200, "SUCCESS", jsons);
    }

    //报价单审批通过
    @RequestMapping(value = "/updatePreOrderState3", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updatePreOrderState3(String objuid) {
        String str = "";
        try {
            PreOrderUpdateDTO preOrderUpdateDTO = new PreOrderUpdateDTO();
            preOrderUpdateDTO.setObjuid(objuid);
            str = preOrderService.updatePreOrderState3(preOrderUpdateDTO);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }

        return new AjaxResult(200, str);
    }

    //报价单审批驳回
    @RequestMapping(value = "/updatePreOrderState2", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updatePreOrderState2(String objuid) {
        String str = "";
        try {
            PreOrderUpdateDTO preOrderUpdateDTO = new PreOrderUpdateDTO();
            preOrderUpdateDTO.setObjuid(objuid);
            str = preOrderService.updatePreOrderState2(preOrderUpdateDTO);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }

        return new AjaxResult(200, str);
    }

    //查询报价单信息（导出）
    @RequestMapping(value = "/queryPreOrderAll3", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult queryPreOrderAll3(String preInquiryUid) throws Exception {
        List preOrderQueryVOList = preOrderService.queryPreOrderAll3(preInquiryUid);

        String jsons = "";
        jsons += "[";
        if (preOrderQueryVOList.size() > 0) {
            for (int i = 0; i < preOrderQueryVOList.size(); i++) {
                PreOrderQueryVO preOrderQueryVO = (PreOrderQueryVO) preOrderQueryVOList.get(i);
                jsons += new Gson().toJson(preOrderQueryVO);
                if (i < (preOrderQueryVOList.size() - 1)) {
                    jsons += ",";
                }
            }
        }
        jsons += "]";

        return new AjaxResult(200, "SUCCESS", jsons);
    }

    //报价单导出
    @RequestMapping(value = "/updatePreOrderState4", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updatePreOrderState4(String objuid) {
        String str = "";
        try {
            PreOrderUpdateDTO preOrderUpdateDTO = new PreOrderUpdateDTO();
            preOrderUpdateDTO.setObjuid(objuid);
            str = preOrderService.updatePreOrderState4(preOrderUpdateDTO);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }

        return new AjaxResult(200, str);
    }

    //报价单生效
    @RequestMapping(value = "/updatePreOrderState6", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updatePreOrderState6(String objuid) {
        String str = "";
        try {
            PreOrderUpdateDTO preOrderUpdateDTO = new PreOrderUpdateDTO();
            preOrderUpdateDTO.setObjuid(objuid);
            str = preOrderService.updatePreOrderState6(preOrderUpdateDTO);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }

        return new AjaxResult(200, str);
    }

    //报价单关闭
    @RequestMapping(value = "/updatePreOrderState7", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updatePreOrderState7(String objuid) {
        String str = "";
        try {
            PreOrderUpdateDTO preOrderUpdateDTO = new PreOrderUpdateDTO();
            preOrderUpdateDTO.setObjuid(objuid);
            str = preOrderService.updatePreOrderState7(preOrderUpdateDTO);
        } catch (Exception e) {
            str = e.getMessage();
            e.printStackTrace();
        }

        return new AjaxResult(200, str);
    }

}
